﻿namespace MonetDbDataProviderTest
{
    using System.Net.Sockets;
    using System.Security.Authentication;

    using MonetDbDataProvider;
    using MonetDbDataProvider.Mapi;

    using NUnit.Framework;

    [TestFixture]
    public class MonetDbConnectionTest
    {
        [Test]
        public void ConnectWithValidCredentialsTest()
        {
            // Arrange
            var connection = new MonetDbConnection("database=demo;host=localhost;port=50000;username=monetdb;password=monetdb;");

            // Act
            connection.Open();

            // Assert
            Assert.IsNotNull(connection, "Socket should not be null");

            connection.Close();
        }

        [Test]
        public void ConnectWithInvalidUsernameTest()
        {
            // Arrange
            var connection = new MonetDbConnection("database=demo;host=localhost;port=50000;username=invalidusername;password=test;");

            // Act & Assert
            Assert.That(() => connection.Open(), Throws.Exception.TypeOf<AuthenticationException>());
        }

        [Test]
        public void ConnectWithInvalidPasswordTest()
        {
            // Arrange
            var connection = new MonetDbConnection("database=demo;host=localhost;port=50000;username=test;password=invalidpassword;");

            // Act & Assert
            Assert.That(() => connection.Open(), Throws.Exception.TypeOf<AuthenticationException>());
        }

        [Test]
        public void ConnectWithInvalidDatabaseTest()
        {
            // Arrange
            var connection = new MonetDbConnection("database=invaliddatabase;host=localhost;port=50000;username=test;password=test;");

            // Act & Assert
            Assert.That(() => connection.Open(), Throws.Exception.TypeOf<MonetDbException>());
        }

        [Test]
        public void ConnectWithInvalidHostTest()
        {
            // Arrange
            var connection = new MonetDbConnection("database=demo;host=invalidhost;port=50000;username=test;password=test;");

            // Act & Assert
            Assert.That(() => connection.Open(), Throws.Exception.TypeOf<SocketException>());
        }

        [Test]
        public void ConnectWithInvalidPortTest()
        {
            // Arrange
            var connection = new MonetDbConnection("database=demo;host=localhost;port=0;username=test;password=test;");

            // Act & Assert
            Assert.That(() => connection.Open(), Throws.Exception.TypeOf<SocketException>());
        }
    }
}